/**
 * @Title: ShopTransRecordController.java
 * @Package com.lemon.controller.cityRun
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年10月22日
 * @version V1.0
 */
package com.lemon.controller.cityRun;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.conf.redis.RedisUtil;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrShops;
import com.lemon.model.cityRun.CrUserTransactionRecord;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.service.cityRun.CrShopsService;
import com.lemon.service.cityRun.CrUserTransactionRecordService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;
import com.lemon.utils.XMLUtils;

/**
 * <p>
 * Title: ShopTransRecordController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年10月22日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/trans")
public class ShopTransRecordController {

	@Resource
	private CrUserTransactionRecordService tranService;
	@Resource
	private CrShopsService shopService;

	@Resource
	private CrShopUserService uService;

	//获取全部记录
	//	@LoginRequired
	@RequestMapping("/getShopTransList")
	public ReqResult getShopTransList(@ModelAttribute CrUserTransactionRecord trans , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			String star = request.getParameter("startTime");
			String end = request.getParameter("endTime");
			StringBuffer buffer = new StringBuffer();

			if (StringUtil.isNotBlank(star) && StringUtil.isNotBlank(end)) {
				buffer.append(" and a.create_time >='");
				buffer.append(star);
				buffer.append("' and a.create_time <='");
				buffer.append(end);
				buffer.append("'");
			} else if (StringUtil.isNotBlank(star)) {
				buffer.append(" and a.create_time >='");
				buffer.append(star);
				buffer.append("'");
			} else if (StringUtil.isNotBlank(end)) {
				buffer.append(" and a.create_time <='");
				buffer.append(end);
				buffer.append("'");
			}

			String ty = request.getParameter("types");
			if (StringUtil.isNotBlank(ty)) {
				if ("1".equals(ty)) {
					buffer.append(" and a.tran_type = 1 and a.type = 2 ");
				} else if ("3".equals(ty)) {
					buffer.append(" and a.tran_type = 2  and a.type=1 ");
				} else if ("2".equals(ty)) {
					buffer.append(" and a.tran_type = 3 ");
				} else if ("4".equals(ty)) {
					buffer.append(" and a.tran_type = 1 and a.type = 1 ");
				}
			}
			buffer.append(" and a.status != 0 ");
			//			if (u.getUserType() == 1) {

			trans.setOrderBy(" order by a.create_time desc");
			trans.setWhere(buffer.toString());
			//查询管理员列表
			List<CrUserTransactionRecord> list = tranService.selectListBySelective(trans);
			reqResult.setReturnResult(list);
			//			}
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改提现管理
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/updateShopTranInfo.do" , method = RequestMethod.POST)
	public ReqResult updateShopTranInfo(@ModelAttribute CrUserTransactionRecord tran , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			String ids = request.getParameter("remAllIds");
			if (StringUtil.isNotBlank(ids)) {
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					String id = split[i];
					tran.setStatus(0);
					tran.setId(id);
					tranService.updateShopTranInfo(tran);
				}
				reqResult.setReturnResult("1");
				return reqResult;
			}
			int row = tranService.updateShopTranInfo(tran);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 新增交易记录
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/addShopTranInfo.do" , method = RequestMethod.POST)
	public ReqResult addShopTranInfo(@ModelAttribute CrUserTransactionRecord tran , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String userid = tran.getShopUserId();
			CrShops cs = new CrShops();
			cs.setShopUserId(userid);
			CrShops shops = shopService.selectShopAndUserBySel(cs);

			CrShopUser u = uService.selectById(shops.getShopUserId());
			if (u.getAmount() < tran.getAmount()) {
				reqResult.setReturnResult("" , ReqResult.code_not_exist);
				return reqResult;
			}
			tran.setSerialNumber(StringUtil.createSerialNoCityRun());
			tran.setStatus(3);
			tran.setAmount(tran.getAmount());
			//读取xml文件获取 提现手续费率
			String string = RedisUtil.get(RedisUtil.key_before + "setting");
			JSONObject js = ((JSONObject) JSONObject.parse(string)).getJSONObject("sets")
					.getJSONObject("enchenchashment");
			Integer sc = js.getInteger("serviceCharge"); //手续费
			Integer minAmount = js.getInteger("minAmount"); //手续费
			if (tran.getAmount() < minAmount) {
				reqResult.setReturnResult("低于最低提现金额,请重新输入" , ReqResult.code_objecr_number);
				return reqResult;
			}
			//
			int ss = (tran.getAmount() * sc) / 100;
			tran.setAmountLeft(u.getAmount());
			tran.setAmountArrival(tran.getAmount() - ss); //这里的3是提现设置里面的 百分之三   因为数据库存入的金额都是  分单位
			tran.setShopAccount(shops.getUserAccount());
			tran.setShopTitle(shops.getShopTitle());
			tran.setShopUserId(shops.getShopUserId());
			tran.setShopUserWx(u.getUserWx());
			tran.setTranType(2);
			tran.setType(1);
			int row = tranService.insertShopTranInfo(tran);
			reqResult.setReturnResult(row);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 新增交易记录
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/getUserInfo" , method = RequestMethod.POST)
	public ReqResult getUserInfo(@ModelAttribute CrUserTransactionRecord tran , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		System.out.println("来到了getuserinfo");
		//		try {
		//			String userid = tran.getShopUserId();
		//			CrShops cs = new CrShops();
		//			cs.setShopUserId(userid);
		//			CrShops shops = shopService.selectShopAndUserBySel(cs);
		//
		//			CrShopUser u = uService.selectById(shops.getShopUserId());
		//			if (u.getAmount() < tran.getAmount()) {
		//				reqResult.setReturnResult("" , ReqResult.code_not_exist);
		//				return reqResult;
		//			}
		//			tran.setSerialNumber(StringUtil.createSerialNoCityRun());
		//			tran.setStatus(3);
		//			tran.setAmount(tran.getAmount());
		//			//读取xml文件获取 提现手续费率
		//			String string = RedisUtil.get(RedisUtil.key_before + "setting");
		//			JSONObject js = ((JSONObject) JSONObject.parse(string)).getJSONObject("sets")
		//					.getJSONObject("enchenchashment");
		//			Integer sc = js.getInteger("serviceCharge"); //手续费
		//			Integer minAmount = js.getInteger("minAmount"); //手续费
		//			if (tran.getAmount() < minAmount) {
		//				reqResult.setReturnResult("低于最低提现金额,请重新输入" , ReqResult.code_objecr_number);
		//				return reqResult;
		//			}
		//			//
		//			int ss = (tran.getAmount() * sc) / 100;
		//			tran.setAmountLeft(u.getAmount() - tran.getAmount());
		//			tran.setAmountArrival(tran.getAmount() - ss); //这里的3是提现设置里面的 百分之三   因为数据库存入的金额都是  分单位
		//			tran.setShopAccount(shops.getUserAccount());
		//			tran.setShopTitle(shops.getShopTitle());
		//			tran.setShopUserId(userid);
		//			tran.setShopUserWx(u.getUserWx());
		//			tran.setTranType(2);
		//			tran.setType(1);
		//			int row = tranService.insertShopTranInfo(tran);
		//			reqResult.setReturnResult(row);
		//		} catch (Exception e) {
		//			reqResult.setReturnResult("" , ReqResult.code_error);
		//			MyException.writeErr(e , "查询客户或管理员列表失败");
		//		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 新增交易记录
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@LoginRequired
	@RequestMapping(value = "/addShopTranSet.do" , method = RequestMethod.POST)
	public ReqResult addShopTranSet(HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String minAmount = request.getParameter("minAmount");
			String serviceCharge = request.getParameter("serviceCharge");
			String enchInfo = request.getParameter("enchInfo");
			if (StringUtil.isNotBlank(enchInfo)) {
				enchInfo = enchInfo.replace("\"" , "'");
			}
			String jsonString = XMLUtils.getXmlToJSONString(XMLUtils.setUrl);
			JSONObject parse = (JSONObject) JSONObject.parse(jsonString);
			JSONObject js = parse.getJSONObject("sets");
			JSONObject js2 = js.getJSONObject("enchenchashment");
			js2.put("minAmount" , minAmount);
			js2.put("serviceCharge" , serviceCharge);
			js2.put("enchInfo" , enchInfo);
			js.put("enchenchashment" , js2);
			parse.put("sets" , js);
			XMLUtils.saveXml(parse.toJSONString() , XMLUtils.setUrl);
			reqResult.setReturnResult(1);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote>
	 * </blockquote></b>
	 * 
	 * @Title: addAdminUserInfo
	 * @param user
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月18日
	 */
	@RequestMapping(value = "/getShopTranSet" , method = RequestMethod.POST)
	public ReqResult getShopTranSet(HttpServletRequest request , HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			String string = RedisUtil.get(RedisUtil.key_before + "setting");
			JSONObject js = ((JSONObject) JSONObject.parse(string)).getJSONObject("sets")
					.getJSONObject("enchenchashment");
			reqResult.setReturnResult(js);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 获取所有商家可提现 和已提现金额
	 * </blockquote></b>
	 * 
	 * @Title: getShopTranProfit
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年11月6日
	 */
	@RequestMapping(value = "/getShopProfit")
	public ReqResult getShopTranProfit(@ModelAttribute CrUserTransactionRecord tran , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			Map<String , Object> list = tranService.getShopTranProfit(tran);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			reqResult.setReturnResult("" , ReqResult.code_error);
			MyException.writeErr(e , "查询客户或管理员列表失败");
		}
		return reqResult;
	}

}
